package com.ayf.payment.game.api.sms;

import com.alibaba.fastjson.JSONObject;
import com.ayf.payment.game.api.dto.MySmsXmlDTO;
import com.ayf.payment.game.api.entity.platform.SmsChannelConfig;
import com.ayf.payment.game.api.entity.platform.SmsContent;
import com.ayf.payment.game.api.entity.platform.SmsRecord;
import com.ayf.payment.game.api.httpclient.HttpUtils;
import com.ayf.payment.game.api.service.platform.SmsContentService;
import com.ayf.payment.game.api.type.SMSType;
import com.ayf.payment.game.api.utils.*;
import com.swwx.charm.commons.lang.utils.LogPortal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.text.MessageFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 自己的短信通道
 */
@Component
public class MySMSHandler extends SMSHandler {

    @Autowired
    SmsContentService smsContentService;

    public void send(String phone, SMSType type, SmsChannelConfig sMSConfig, Integer merchantId) {

        JSONObject config = JSONObject.parseObject(sMSConfig.getChannelConfig());
        final String url = config.getString("url");
        final String account = config.getString("account");
        final String password = config.getString("password");
        final String smsSign = config.getString("sign");
        final String extend = config.getString("extend");

        SmsContent smsContent = smsContentService.getSmsContentBySmsChannelCodeAndSmsType(sMSConfig.getChannelCode(),type.name());
        String smsMsg = type.getDesc();
        if (smsContent != null){
            smsMsg = smsContent.getSmsContent();
        }
        String code = RandomStrUtils.createRandom(true, 6);
        String msg = smsSign.concat(MessageFormat.format(smsMsg,code));
        String currentTimeStamp = DateUtils.dateToString(new Date(), DateUtils.formatPattern_full_01);
        Map<String, String> data = new HashMap<>();
        data.put("account", account);
        data.put("timestamp",currentTimeStamp);
        data.put("password",MD5Util.strToMd5(password.concat(currentTimeStamp)));
        data.put("mobile", phone);
        String traceId = SnowFlakeUtils.getTraceId();
        try {
            data.put("content",msg);
            data.put("extend", extend);
            Map<String, String> map = HttpUtils.smsHttpPost(traceId, url, data);
            String date = map.get(HttpUtils.RESPONSE_DATA);
            MySmsXmlDTO dto = KqXmlUtil.getObjectFromXML("returnsms", date, MySmsXmlDTO.class);
            SmsRecord smsRecord = new SmsRecord();
            if (map.get(HttpUtils.RESPONSE_CODE).equals("200")) {
                if ("Success".equals(dto.getReturnstatus())) {
                    smsRecord.setStatus(1);
                    LogPortal.info("[{}]易迅通短信验证码发送成功,发送手机号:[{}],参数:[{}],返回结果:[{}]", traceId, phone, data, map);
                } else {
                    smsRecord.setStatus(0);
                    LogPortal.error("[{}]易迅通短信验证码发送失败,发送手机号:[{}]发送内容为:[{}],原因为:[{}]", traceId, phone, msg, dto.getMessage());
                }
                smsRecord.setSendPhone(phone);
                smsRecord.setSmsChannel(sMSConfig.getChannelName());
                smsRecord.setSendContent(msg);
                smsRecord.setChannelMsgid(dto.getTaskID());
                smsRecord.setResponseCode(dto.getReturnstatus());
                smsRecord.setResponseCodeMsg(dto.getMessage());
            } else {
                smsRecord.setStatus(0);
                smsRecord.setResponseCodeMsg("请求易迅通短信接口失败");
                LogPortal.error("[{}]易迅通短信验证码发送失败,发送手机号:[{}]发送内容为:[{}],调用接口失败", traceId, phone,msg);
            }
            save(smsRecord, type, code, merchantId);
        } catch (IOException e) {
            LogPortal.error("[{}]易迅通短信验证码发送失败,发送手机号:[{}]发送内容为:[{}],异常为[{}]", traceId, phone,msg,e.getMessage());
        }
    }
}
